package com.xhd.codec;

import java.io.ByteArrayOutputStream;

import javax.mail.internet.MimeUtility;

public class MimeCoder
{
  public static String decodeName(String paramString)
  {
    if ((paramString == null) || (paramString.length() < 5))
      return paramString;
    try
    {
      return MimeUtility.decodeText(paramString);
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    return paramString;
  }

  public static String encodeName(String paramString)
  {
    if (paramString == null)
      return "";
    try
    {
      return MimeUtility.encodeText(paramString, "UTF-8", "B");
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    return paramString;
  }

  public static String qpDecoding(String paramString1, String paramString2)
  {
    if (paramString1 == null)
      return "";
    try
    {
      paramString1 = paramString1.replaceAll("=\n", "");
      byte[] arrayOfByte = paramString1.getBytes("US-ASCII");
      for (int i = 0; i < arrayOfByte.length; ++i)
      {
       byte  j = arrayOfByte[i];
        if (j != 95)
          arrayOfByte[i] = j;
        else
          arrayOfByte[i] = 32;
      }
      if (arrayOfByte == null)
        return "";
      ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
      for (int j = 0; j < arrayOfByte.length; ++j)
      {
        int k = arrayOfByte[j];
        if (k == 61)
          try
          {
            int l = Character.digit((char)arrayOfByte[(++j)], 16);
            int i1 = Character.digit((char)arrayOfByte[(++j)], 16);
            if (l != -1)
              if (i1 != -1)
                localByteArrayOutputStream.write((char)((l << 4) + i1));
          }
          catch (ArrayIndexOutOfBoundsException localArrayIndexOutOfBoundsException)
          {
            localArrayIndexOutOfBoundsException.printStackTrace();
          }
        else
          localByteArrayOutputStream.write(k);
      }
      return new String(localByteArrayOutputStream.toByteArray(), paramString2);
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    return "";
  }
}
